package com.xn.app.muying.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.xn.app.muying.R;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.MyAlertDialog;
import com.xn.app.muying.util.ToastUtil;

import java.io.IOException;

import okhttp3.Request;

/**
 * Created by Administrator on 2016/10/19.
 */
public class YuChanJiSuanActivity extends BaseActivity implements View.OnClickListener {
    private EditText et;
    //数据库
    private DBManager db=null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yuchanjisuan);
        db=new DBManager(this);
        ImageView back = (ImageView) findViewById(R.id.yuchanjisuan_activity_back);
        et = (EditText) findViewById(R.id.yuchanjisuan_activity_et);
        Button btn = (Button) findViewById(R.id.yuchanjisuan_activity_btn);


        back.setOnClickListener(this);
        btn.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.yuchanjisuan_activity_back:
                finish();
                break;

            case R.id.yuchanjisuan_activity_btn:
                float date = Float.parseFloat(et.getText().toString().trim());
                yuchanjisuan(date);

                break;
        }
    }
    AlertDialog dialog;

    private void yuchanjisuan(final float date) {
        dialog= new MyAlertDialog().ShowDialog(YuChanJiSuanActivity.this, R.layout.dialog_loading);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Request request = new Request.Builder().
                            url(APIMethod.YunChanJiSuan(date)).
                            addHeader("Authorization",db.select("token_type")+" " + db.select("access_token")).get().build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();

                    int code = response.code();
                    String bodys = response.body().string();
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler.sendMessage(msg2);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1 = code;
                        msg1.obj = bodys;
                        handler.sendMessage(msg1);
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(dialog!=null){
                dialog.dismiss();
            }
            switch (msg.what) {
                case 0:
                    ToastUtil.showToast(YuChanJiSuanActivity.this, "设置成功");
                    break;
                case 1:
                    ToastUtil.showToast(YuChanJiSuanActivity.this, "设置失败");
                    break;
            }
            Intent intent = new Intent();
            intent.putExtra("shijian", msg.obj.toString());
            setResult(4, intent);
            finish();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
    }
}

